package com.example.cinema.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.cinema.entity.BizExeption;
import com.example.cinema.entity.Movie;
import com.example.cinema.entity.Result;
import com.example.cinema.entity.Studios;
import com.example.cinema.entity.service.IStudiosService;
import com.example.cinema.mapper.StudiosMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yc138
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/studios")
public class StudiosController {
    @Autowired
    IStudiosService studiosService;
    @RequestMapping("addStudios")
    public Result addStudios(@RequestBody @Valid Studios studios, Errors errors){
        if(errors.hasErrors()){
            return new Result(0,"影院信息验证错误",errors.getAllErrors());
        }
        try {
            studiosService.addStudios(studios);
            return new Result(1,"影院新增成功",null);
        } catch (BizExeption e) {
            errors.rejectValue(e.getData().toString(),"??",e.getMessage());
            return new Result(0,"影院信息验证错误",errors.getAllErrors());
        }
    }
    @RequestMapping("updateStudios")
    public Result updateStudios(@RequestBody @Valid Studios studios, Errors errors){
        if(errors.hasErrors()){
            return new Result(0,"影院信息验证错误",errors.getAllErrors());
        }
        try {
            studiosService.updateStudios(studios);
            return new Result(1,"编辑信息成功",null);
        } catch (BizExeption e) {
            errors.rejectValue(e.getData().toString(),"??",e.getMessage());
            return new Result(0,"影院信息验证错误",errors.getAllErrors());
        }
    }

    @RequestMapping("del")
    public Result del(@RequestBody Studios studios){
        if(studios.getStatus().equals("营业中")){
            studios.setStatus("休息中");
        }else if(studios.getStatus().equals("休息中")){
            studios.setStatus("营业中");
        }
        int ret = studiosMapper.updateById(studios);
        if(ret > 0){
            return new Result(1,"影院状态修改成功",null);
        }else{
            return new Result(0,"影院状态修改失败",null);
        }
    }


    @Autowired
    StudiosMapper studiosMapper;
    @RequestMapping("queryStudios")
    public Page<Studios> queryStudios(@RequestBody Studios studios,
                                    @RequestParam(defaultValue = "1") int current,
                                    @RequestParam(defaultValue = "5")int size){
        LambdaQueryWrapper<Studios> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Studios::getId);
        if(studios.getName()!=null && studios.getName().isBlank()==false){
            lqw.like(Studios::getName, studios.getName());
        }
        if(studios.getAddr()!=null && studios.getAddr().isBlank()==false){
            lqw.like(Studios::getAddr, studios.getAddr());
        }
        if(studios.getStatus()!=null && studios.getStatus().isBlank()==false){
            lqw.like(Studios::getStatus, studios.getStatus());
        }
        Page<Studios> p = new PageDTO<>(current, size);
        Page<Studios> studiosPage = studiosMapper.selectPage(p, lqw);
        return studiosPage;
    }

    @RequestMapping("queryStu")
    public List<Studios>  queryStu(@RequestParam(defaultValue = "1") int current,
                                   @RequestParam(defaultValue = "10")int size){
        List<Studios> list = studiosMapper.queryStudios((current-1)*size,size);
        LambdaQueryWrapper<Studios> lqw = new LambdaQueryWrapper<>();
        List<Studios> studiosList = studiosMapper.selectList(lqw);
        for (Studios studios : list) {
            studios.setTotal(studiosList.size());
        }
        return list;
    }
}

